package com.example.yls.demoa;

import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private MypageViewAdapter mypageViewAdapter;
private ArrayList<Fragment> fragments=new ArrayList<>();
private TextView tv_message;
private TextView tv_contact;
private TextView tv_find;
private TextView tv_my;
private TabLayout tableLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         initData();
         initView();
         tv_messageEvent();
         tv_contactEvent();
         tv_findEvent();
         tv_myEvent();
         viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
             @Override
             public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

             }

             @Override
             public void onPageSelected(int position) {

                 switch (position){
                     case 0:
                         tv_message.setBackgroundColor(Color.RED);
                         tv_contact.setBackgroundColor(Color.GREEN);
                         tv_find.setBackgroundColor(Color.GREEN);
                         tv_my.setBackgroundColor(Color.GREEN);

                         break;
                     case 1:
                         tv_message.setBackgroundColor(Color.GREEN);
                         tv_contact.setBackgroundColor(Color.RED);
                         tv_find.setBackgroundColor(Color.GREEN);
                         tv_my.setBackgroundColor(Color.GREEN);
                         break;
                     case 2:
                         tv_message.setBackgroundColor(Color.GREEN);
                         tv_contact.setBackgroundColor(Color.GREEN);
                         tv_find.setBackgroundColor(Color.RED);
                         tv_my.setBackgroundColor(Color.GREEN);
                         break;
                     case 3:
                         tv_message.setBackgroundColor(Color.GREEN);
                         tv_contact.setBackgroundColor(Color.GREEN);
                         tv_find.setBackgroundColor(Color.GREEN);
                         tv_my.setBackgroundColor(Color.RED);
                         break;
                 }

             }

             @Override
             public void onPageScrollStateChanged(int state) {

             }
         });


    }



    private void tv_messageEvent() {
        tv_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(0);
            }
        });

    }
    private void tv_contactEvent() {
        tv_contact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(1);
            }
        });

    }
    private void tv_findEvent() {
        tv_find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(2);
            }
        });

    }
    private void tv_myEvent() {
        tv_my.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(3);
            }
        });

    }

    private void initData() {
         messageFramgment messageFramgment=new messageFramgment();
         contactFramgment contactFramgment=new contactFramgment();
         findFramgment findFramgment=new findFramgment();
         MyFramgment myFramgment=new MyFramgment();

         fragments.add(messageFramgment);
         fragments.add(contactFramgment);
         fragments.add(findFramgment);
         fragments.add(myFramgment);

    }

    private void initView() {

      viewPager=findViewById(R.id.vp_content);
      mypageViewAdapter=new MypageViewAdapter(getSupportFragmentManager(),fragments);

      viewPager.setAdapter(mypageViewAdapter);

      tv_message=findViewById(R.id.tv_message);
      tv_contact=findViewById(R.id.tv_contact);
      tv_find=findViewById(R.id.tv_find);
      tv_my=findViewById(R.id.tv_my);

        tv_message.setBackgroundColor(Color.RED);
        tv_contact.setBackgroundColor(Color.GREEN);
        tv_find.setBackgroundColor(Color.GREEN);
        tv_my.setBackgroundColor(Color.GREEN);
        tableLayout=findViewById(R.id.tl_tab);

        tableLayout.setupWithViewPager(viewPager);

    }


}
